<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 搜索导航模块 -->
 <el-form  label-width="80px" inline>
  <el-form-item label="关键字">
    <el-input  v-model="queryInfo.keyword" placeholder='根据文章标题搜索'></el-input>
  </el-form-item>
  <el-form-item label="状态">
    <el-select v-model="selet" placeholder="请选择">
      <el-option label="启用" value="1"></el-option>
      <el-option label="禁用" value="2"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button size="mini" @click="seachForm=null">清除</el-button>
    <el-button type="primary" size="mini" @click="getUserList">搜索</el-button>
  </el-form-item>
  <el-button class="addBtn" type="success" icon="el-icon-edit" size="small" @click="putdata()" >新增技巧</el-button>
 </el-form>
 <!-- 消息提示模块 -->
   <el-alert
    title="数据一共 34 条"
    type="info"
    show-icon
    :closable="false">
  </el-alert>
  <!-- tab表格模块 -->
   <el-table
      :data="userlist"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="序号"
        width="80"
        type="index">
      </el-table-column>
      <el-table-column
        label="文章标题"
        width="400">
      <template slot-scope="scope">
      {{scope.row.title}}
      <i v-if="scope.row.videoURL" class="el-icon-film video-film" @click="videoShow(scope.row)">
      </i>

    </template>
      </el-table-column>
      <el-table-column
        prop="visits"
        label="阅读数"
        width="301">
      </el-table-column>
      <el-table-column
        prop="username"
        label="录入人"
        width="300">
      </el-table-column>
      <el-table-column
        label="录入时间"
        width="300">
        <template slot-scope="scope">
          {{scope.row.createTime|parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">已启用</span>
          <span v-else-if="scope.row.state == 0">已禁用</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180">
        <template slot-scope="scope" >
          <div class="background">
          <el-link :underline="false"  type="primary" @click="yulan(scope.row)">预览</el-link>
          <el-link :underline="false"  type="primary"  v-if="scope.row.state == 1"  @click="caozuo(scope.row)">启用</el-link>
          <el-link :underline="false"  type="primary"  v-else  @click="caozuo(scope.row)">禁用</el-link>
          <el-link :underline="false"  type="primary"  v-if="scope.row.state == 1" @click="putdata(scope.row)">修改</el-link>
          <el-link :underline="false"  type="info" v-else disabled >修改</el-link>
          <el-link :underline="false"  type="primary"  v-if="scope.row.state == 1" @click="deletBtn(scope.row)">删除</el-link>
          <el-link :underline="false"  type="info" v-else disabled>删除</el-link>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>
<!-- 新增技巧对话框 -->
<el-dialog
  :title="this.flag ? '修改文章' : '新增文章'"
  :visible.sync="isShowDialogVisible"
  width="40%">
<el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" class="demo-ruleForm">
  <el-form-item label="文章标题" prop="title">
    <el-input v-model="addForm.title"></el-input>
  </el-form-item>
   <el-form-item label="文章内容" prop="articleBody">
    <quill-editor v-model="addForm.articleBody"></quill-editor>
  </el-form-item>
    <el-form-item label="视频地址" prop="videoURL">
    <el-input v-model="addForm.videoURL"></el-input>
  </el-form-item>

</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="delBtn">取 消</el-button>
    <el-button type="primary" @click="sureBtn">确 定</el-button>
  </span>
</el-dialog>

<!-- 预览对话框 -->
<el-dialog
  title="预览文章"
  :visible.sync="isYulanDialogVisible"
  width="50%">
  <h2>{{row.title}}</h2>
  <div class="row">
    <span>{{row.createTime|parseTimeByString}}</span>
    <span>{{row.username}}</span>
    <span class="el-icon-view"></span>
    <span>{{row.visits}}</span>
  </div>
  <div class="row-div">
    <span>{{row.title}}</span>
  </div>
</el-dialog>
<!-- 外层的遮罩层区域 -->
<div
  class="mask"
  v-if="videoState"
>
<el-button
  type='danger'
  circle
  @click="closedVideo"
  class='closedbtn el-icon-close'
 ></el-button>
</div>
<!-- 弹窗 -->
<div
  class="videomasks"
  v-if="videoState == true"
>
 <video
 :src="videoSrc"
 controls = "controls"
 autoplay
 muted
 >
 </video>
</div>
  </div>
</template>

<script>
import { list, remove, add, update } from '@/api/hmmm/articles'
export default {
  data () {
    return {
      // 表单数据
      seachForm: [],
      // 列表数据
      userlist: [],
      // 总条数
      total: 0,
      // 获取列表的参数对象
      queryInfo: {
        page: 1,
        pagesize: 10,
        keyword: null
      },
      // 新增技巧的对象
      addForm: {
        // 文章标题
        title: '',
        // 文章内容
        articleBody: '',
        // 视频地址
        videoURL: ''
      },
      flag: Boolean,
      // 新增技巧的表单验证规则
      addFormRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      selet: '',
      // 控制新增技巧对话框的显示与隐藏
      isShowDialogVisible: false,
      // 控制预览对话框的显示与隐藏
      isYulanDialogVisible: false,
      // 预览参数
      row: '',

      // 接收修改的数据
      jieshouxiugai: null,
      // 控制视频遮罩层的显示与隐藏
      videoState: false,
      // 视频的路径,默认为空
      videoSrc: ''

    }
  },
  components: {
  },

  created () {
    this.getUserList()
  },
  methods: {
    async getUserList () {
      const { data } = await list(this.queryInfo)
      // console.log(data)
      this.userlist = data.items
      this.total = data.counts
    },
    // 监听每页显示多少条数据事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    // 监听第几页事件
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getUserList()
    },
    // 点击启用禁用事件
    caozuo (scope) {
      scope.state = !scope.state
      this.$message.success('操作成功')
    },
    // 点击确定,验证规则
    async sureBtn () {
      if (this.flag) {
        await update(this.addForm)
        this.$message.success('修改文章成功')
        this.isShowDialogVisible = false
        this.$refs.addFormRef.resetFields()
        // 重新加载页面
        this.getUserList()
      } else {
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return
          await add(this.addForm)
          this.$message.success('添加文章成功')
          this.isShowDialogVisible = false
          this.$refs.addFormRef.resetFields()
          // 重新加载页面
          this.getUserList()
        })
      }
    },
    // 点击取消,关闭对话框并重置表单

    delBtn () {
      this.isShowDialogVisible = false
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮预览展示对话框
    yulan (row) {
      this.isYulanDialogVisible = true
      console.log(row)
      this.row = row
    },
    // 点击删除按钮事件
    async deletBtn (id) {
      await remove(id)
      this.$message.success('删除成功')
      // 刷新页面
      this.getUserList()
    },
    // // 点击修改按钮展示对话框
    async putdata (row) {
      this.isShowDialogVisible = true
      if (row) {
        console.log(row)
        this.addForm = row
        this.flag = true
      } else {
        this.addForm = {}

        this.flag = false
      }
    },
    // 点击图标, 播放视频
    videoShow (scope) {
      console.log(scope)
      // 对视频的路径赋值
      this.videoSrc = scope.videoURL
      // 点击显示视频弹出框
      this.videoState = true
    },
    // 关闭视频遮罩层的操作
    closedVideo () {
      this.videoState = false
    }
  }

}

</script>

<style scoped lang='scss'>
.addBtn{
  float:right
}
.container{
  padding: 15px;
}
.el-link {
  margin: 0 5px
}
.el-table{
  margin: 20px 0;
}
.el-pagination{
  float: right;
}
.row span {
  margin-left: 10px;
}
.row-div {
  height: 64px;
  background: #f5f5f5;
  padding: 10px;
  color: #606266;
  font-size: 14px;
  border-top: 1px dotted #000;
  margin-top: 10px;
}
::v-deep .ql-editor {
  min-height: 200px !important;
}
.video-film {
  color: #00f;
  font-size: 18px;
}
// 遮罩层样式
.mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: #000000;
  opacity: 0.6;
  z-index: 8888;
}
// 视频弹窗样式
.videomasks {
  position: fixed;
  min-width: 1200px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9999;
}
// 视频的相关样式
.videomasks video {
  width: 100%;
  height: 100%;
}
// 按钮相关的样式
.closedbtn {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 30px;
}
</style>
